#!/usr/local/swan/opt/php/bin/php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// +---------------------------------------------------------------------------
// | SWAN [ $_SWANBR_SLOGAN_$ ]
// +---------------------------------------------------------------------------
// | Copyright $_SWANBR_COPYRIGHT_$
// +---------------------------------------------------------------------------
// | Version  $_SWANBR_VERSION_$
// +---------------------------------------------------------------------------
// | Licensed ( $_SWANBR_LICENSED_URL_$ )
// +---------------------------------------------------------------------------
// | $_SWANBR_WEB_DOMAIN_$
// +---------------------------------------------------------------------------
 
require_once 'dev_core.php';
//引用SWANSOFT的核心定义文件
require_once D_PATH_SWAN_SOFT . 'core.php';
require_once D_PATH_SWAN_LIB . 'iterator/sw_iterator_fetch_dir.class.php';

/**
+------------------------------------------------------------------------------
* 制作打包的tar源码包 
+------------------------------------------------------------------------------
* 
* @package 
* @version $_SWANBR_VERSION_$
* @copyright $_SWANBR_COPYRIGHT_$
* @author $_SWANBR_AUTHOR_$ 
+------------------------------------------------------------------------------
*/
class build_tar
{
	// {{{ const

	/**
	 * 需要构建的软件目录  
	 */
	const SWANSOFT_GIT_DIR = '/root/code/swansoft/';

	// }}} 
	// {{{ members
	
	/**
	 * 需要忽略的目录 
	 * 
	 * @var array
	 * @access protected
	 */
	protected $__ignore_dir = array(
		'.git/.',
		'test/.',
		'c/.',
	);

	// }}}
	// {{{ functions
	// {{{ public function run()

	public function run()
	{
		$dir_name = $this->_get_output_dir();
		if (file_exists($dir_name) && is_dir($dir_name)) {
			throw new Exception("this dir $dir_name has exists. ");	
		}

		$is_mkdir = mkdir($dir_name);
		if (!$is_mkdir) {
			throw new Exception("mkdir $dir_name failed. ");	
		}
		$this->_process_files();	
		$this->_create_tar();
	}

	// }}}
	// {{{ protected function _get_output_dir()

	/**
	 * 获取并创建输出的目录 
	 * 
	 * @access protected
	 * @return string
	 */
	protected function _get_output_dir()
	{
		$dir_name = './' . SWAN_SOFTNAME . '-' . SWAN_VERSION . '/';

		return $dir_name;
	}

	// }}}
	// {{{ protected function _process_files()

	/**
	 * 处理所有的打包文件 
	 * 
	 * @access protected
	 * @return array
	 */
	protected function _process_files()
	{
		$dir_iterator = new sw_iterator_fetch_dir(self::SWANSOFT_GIT_DIR);
		$dir_iterator->set_ignore_dir($this->__ignore_dir);	
		foreach ($dir_iterator as $file_path => $value)
		{
			if ($value->isFile()) {
				$this->_repalce_file($file_path);				
			}	
		}
	}

	// }}}
	// {{{ protected function _repalce_file()

	/**
	 * 替换文件 
	 * 
	 * @access protected
	 * @return void
	 */
	protected function _repalce_file($path)
	{
		$file_path = self::SWANSOFT_GIT_DIR . $path;
		$out_put_file = $this->_get_output_dir();
		if (!file_exists($file_path)) {
			throw new Exception("this file $file_path not exitst. ");	
		}

		//  {{{ 替换参数 
		$search_array = array(
			'$_SWANBR_SLOGAN_$',
			'$_SWANBR_COPYRIGHT_$',
			'$_SWANBR_VERSION_$',
			'$_SWANBR_LICENSED_URL_$',
			'$_SWANBR_WEB_DOMAIN_$',
			
			'$_SWANBR_AUTHOR_$',
		);

		$replace_array = array(
			SWANBR_SLOGAN,
			SWANBR_COPYRIGHT,
			SWAN_VERSION,
			SWANBR_LICENSED_URL,
			SWANBR_WEB_DOMAIN,
			
			SWANBR_AUTHOR,
		);

		// }}}
		
		$string = file_get_contents($file_path);
		$output = str_replace($search_array, $replace_array, $string);
		
		$filename = $out_put_file . $path;

		$dir_name = dirname($filename);
		if (!file_exists($dir_name)) {
			if (!mkdir($dir_name, 0755, true)) {
				throw new Exception("mkdir output file failed. ");	
			}	
		}

		if (!$handle = fopen($filename, 'a')) {
			throw new Exception("open output file failed. ");	
		}

		// 将$somecontent写入到我们打开的文件中。
		if (false === fwrite($handle, $output)) {
			throw new Exception("write output file failed. ");	
		}

		fclose($handle);
	}

	// }}}
	// {{{ protected function _create_tar()

	/**
	 * 创建打包文件 
	 * 
	 * @access protected
	 * @return void
	 */
	protected function _create_tar()
	{
		$out_dir = $this->_get_output_dir();		
		$cmd = 'tar -zcf ' . SWAN_SOFTNAME . '-' . SWAN_VERSION . '.tar ' . $out_dir;  
		exec($cmd, $return_val, $return_status);
		if (0 !== $return_status) {
			throw new Exception("create tar failed. ");	
		}
		$rm_cmd = 'rm -rf ' . $out_dir;
		unset($return_val);
		exec($rm_cmd, $return_val, $return_status);
		if (0 !== $return_status) {
			throw new Exception("remove dir $out_dir failed. ");	
		}
	}

	// }}}
	// }}}
}

try {
	$build_tar = new build_tar();
	$build_tar->run();
} catch (Exception $e) {
	echo $e->getMessage() . PHP_EOL;	
}
?>
